﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using Ionic.Zip;
using WMI_PHP_OBFS;
using System.Diagnostics;
using System.Reflection;
using System.Threading;

namespace PhpCipher
{
    public partial class frmmain : Form
    {
        WMI_PHP_LOCK php = new WMI_PHP_LOCK();
        private bool _isExpri { get; set; }
        private bool _isIP { get; set; }
        private bool _isDomain { get; set; }
        private bool _isCopy { get; set; }

        private DateTime _ExpDate { get; set; }
        private string _Domain { get; set; }
        private string _IP_Add { get; set; }
        private int __Prefix_Len { get; set; }
        private string __Copy { get; set; }
        private string _bak_loc { get; set; }

        BackgroundWorker bw = new BackgroundWorker();
        

        public frmmain()
        {
            InitializeComponent();
        }

        private void frmmain_Load(object sender, EventArgs e)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.FileVersion;
            lb_ver.Text = "Ver "+version;
        }
        #region BUTTON ACTION
        private void chk_copy_CheckedChanged(object sender, EventArgs e)
        {
            _isCopy = (chk_copy.CheckState == CheckState.Checked) ? true : false;
            txt_copy.Enabled = (chk_copy.CheckState == CheckState.Checked) ? true : false;
        }

        private void chk_ip_CheckedChanged(object sender, EventArgs e)
        {
            _isIP = (chk_ip.CheckState == CheckState.Checked) ? true : false;
            txt_ip.Enabled = (chk_ip.CheckState == CheckState.Checked) ? true : false;
        }

        private void chk_domain_CheckedChanged(object sender, EventArgs e)
        {
            _isDomain = (chk_domain.CheckState == CheckState.Checked) ? true : false;
            txt_domain.Enabled = (chk_domain.CheckState == CheckState.Checked) ? true : false;
        }

        private void chk_time_CheckedChanged(object sender, EventArgs e)
        {
            _isExpri = (chk_time.CheckState == CheckState.Checked) ? true : false;
            txt_time.Enabled = (chk_time.CheckState == CheckState.Checked) ? true : false;
        }

        private void btn_load_file_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Php Files(*.php,*.php4,*.php5)|*.php|*.php4|*.php5";

            if(dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK){
                _bak_loc = Path.GetDirectoryName(dlg.FileName) + "\\";
                lst_file.Items.Add(dlg.FileName);
                
            }
        }

        private void txt_time_ValueChanged(object sender, EventArgs e)
        {
            _ExpDate = DateTime.Parse(txt_time.Value.ToString("yyyy-MM-dd"));
        }

        private void txt_copy_TextChanged(object sender, EventArgs e)
        {
            __Copy = txt_copy.Text;
        }

        private void txt_ip_TextChanged(object sender, EventArgs e)
        {
            _IP_Add = txt_ip.Text;
        }

        private void txt_domain_TextChanged(object sender, EventArgs e)
        {
            _Domain = txt_domain.Text;
        }

        private void btn_load_folder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                //lst_file.Items.Clear();
                _bak_loc = folderBrowserDialog1.SelectedPath + "\\";
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.php", SearchOption.AllDirectories);
                foreach (string file in files)
                {
                    lst_file.Items.Add(file);
                }
            }
        }
        private void btn_clear_Click(object sender, EventArgs e)
        {
            lst_file.Items.Clear();
        }
        #endregion

        private void btn_obfus_Click(object sender, EventArgs e)
        {
            
            if(chk_backup.CheckState == CheckState.Checked){
                __Log("Createing zip backup..........");

                using (ZipFile zip = new ZipFile())
                {
                    
                    for(int i=0;i<lst_file.Items.Count;i++){
                        zip.AddFile(lst_file.Items[i].ToString());
                    }
                    zip.Save(_bak_loc+ "Backup_php_"+DateTime.Now.ToString("yyyy-MM-dd")+".zip");
                }
                __Log("Backup done!");
            }
            __Log("Php ini option......");
            php._isCopy = _isCopy;
            php.__Copy = txt_copy.Text;

            php._isExpri = _isExpri;
            php._ExpDate = _ExpDate;
            
            php._isDomain = _isDomain;
            php._Domain = txt_domain.Text;

            php._isIP = _isIP;
            php._IP_Add = txt_ip.Text;
            
            
            php.__Prefix_Len =  Convert.ToInt32(txt_valid.Text);

            
            __Log("Init thread........");
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_completed);
            __Log("Working......");
            bw.RunWorkerAsync();
            
            btn_obfus.Enabled = false;
            

            //php.PHP_AUTO(lst_file.Items[0].ToString());

        }

        private void bw_completed(object sender, RunWorkerCompletedEventArgs e)
        {
            __Log("COMPLETED!");
            MessageBox.Show("Done!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Information);
            btn_obfus.Enabled = true;
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            //int pc = 0;
            try
            {
                for (int i = 0; i < lst_file.Items.Count; i++)
                {
                    //Thread.Sleep(100);
                    php.PHP_AUTO(lst_file.Items[i].ToString());
                    __Log("SUCCESS => " + lst_file.Items[i].ToString());
                }
                
            }
            catch (Exception ex)
            {
                __Log(ex.Message);
            }
            //pc += 100 / lst_file.Items.Count;
            //bw.ReportProgress(pc);
             //50 is percentage
        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //make sure ProgressBar is not continuous, and has a max value
            //pr_proc.Value = e.ProgressPercentage;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://tungpc.com");
        }

        private void lb_ver_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://phpcipher.net");
        }

        private void __Log(string _log) {
            txt_console.Text += _log + Environment.NewLine + "-----------------------" + Environment.NewLine;
            txt_console.SelectionStart = txt_console.Text.Length;
            txt_console.ScrollToCaret();
        }

        public int StrStr(string value, string searchArgument)
        {
            if (value == null) { throw new ArgumentNullException("value"); }
            if (searchArgument == null) { throw new ArgumentNullException("searchArgument"); }
            if (searchArgument.Length == 0) { return 0; }

            int searchLength = searchArgument.Length;
            int length = value.Length;

            if (searchLength > length) { return -1; }

            for (int i = 0; i < length; i++)
            {
                if (length - i < searchLength) { return -1; }

                if (IsMatchAtIndex(value, searchArgument, i)) { return i; }
            }
            return -1;
        }

        private bool IsMatchAtIndex(String value, String searchArgument, int startIndex)
        {
            for (int j = 0; j < searchArgument.Length; j++)
            {
                if (value[startIndex + j] != searchArgument[j])
                {
                    return false;
                }
            }
            return true;
        } 

    }
}
